#include "debug_show.h"

namespace rmd {
DebugShow* DebugShow::instance_ = NULL;
DebugShow* DebugShow::Instance() {
  if (instance_ == NULL) {
    instance_ = new DebugShow();
  }
  return instance_;
}

void DebugShow::debugCaptureImage(char* image_path, int picture_ID,
                                  char* pBaseDir, short int snIsShow) {
  Mat mSrcImageData = imread(image_path);

  char imgPath[256];

  sprintf(imgPath, "%s\\0CaptureImage\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("0CaptureImage", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("0CaptureImage", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }

  waitKey(10);
}

void DebugShow::debugInputImage(Mat mSrcImageData, int picture_ID,
                                char* pBaseDir, short int snIsShow,
                                int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);

  char imgPath[256];

  sprintf(imgPath, "%s\\0Input\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("0Input", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("0Input", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugGrayImage(Mat mSrcImageData, int picture_ID,
                               char* pBaseDir, short int snIsShow,
                               int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);

  cv::namedWindow("1Gray", CV_WINDOW_NORMAL);
cv:
  resizeWindow("1Gray", showWidth, showHeight);

  char imgPath[256];

  sprintf(imgPath, "%s\\1Gray\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("1Gray", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("1Gray", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugIPMImage(Mat mSrcImageData, int picture_ID, char* pBaseDir,
                              short int snIsShow, int showWidth,
                              int showHeight) {
  char imgPath[256];

  sprintf(imgPath, "%s\\0IPM\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("0IPM", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("0IPM", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugFeature(Mat mSrcImageData, int picture_ID, char* pBaseDir,
                             short int snIsShow, int showWidth,
                             int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);

  cv::namedWindow("2Feature", CV_WINDOW_NORMAL);
cv:
  resizeWindow("2Feature", showWidth, showHeight);

  char imgPath[256];

  sprintf(imgPath, "%s\\2Feature\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("2Feature", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("2Feature", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugFeatureIPM(Mat mSrcImageData, int picture_ID,
                                char* pBaseDir, short int snIsShow,
                                int showWidth, int showHeight) {
  char imgPath[256];

  sprintf(imgPath, "%s\\2FeatureIPM\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("2FeatureIPM", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("2FeatureIPM", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugHoughLine(Mat mSrcImageData, int picture_ID,
                               char* pBaseDir, short int snIsShow,
                               int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);
  cv::namedWindow("3HoughLine", CV_WINDOW_NORMAL);
cv:
  resizeWindow("3HoughLine", showWidth, showHeight);

  char imgPath[256];

  sprintf(imgPath, "%s\\3HoughLine\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("3HoughLine", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("3HoughLine", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugCluterLine(Mat mSrcImageData, int picture_ID,
                                char* pBaseDir, short int snIsShow,
                                int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);

  cv::namedWindow("4CluterLine", CV_WINDOW_NORMAL);
cv:
  resizeWindow("4CluterLine", showWidth, showHeight);

  char imgPath[256];

  sprintf(imgPath, "%s\\4CluterLine\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("4CluterLine", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("4CluterLine", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugFrameAssociation(Mat mSrcImageData, int picture_ID,
                                      char* pBaseDir, short int snIsShow,
                                      int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);

  cv::namedWindow("5debugFrameAssociation", CV_WINDOW_NORMAL);
cv:
  resizeWindow("5debugFrameAssociation", showWidth, showHeight);

  char imgPath[256];

  sprintf(imgPath, "%s\\5debugFrameAssociation\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("5debugFrameAssociation", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("5debugFrameAssociatione", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugResultShow(Mat mSrcImageData, int picture_ID,
                                char* pBaseDir, short int snIsShow,
                                int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);
  cv::namedWindow("6debugResultShow", CV_WINDOW_NORMAL);
cv:
  resizeWindow("6debugResultShow", showWidth, showHeight);

  char imgPath[256];

  sprintf(imgPath, "%s/6debugResultShow/%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("6debugResultShow", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("6debugResultShow", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::showImageSpine(Mat mSrcImageData, int picture_ID,
                               char* pBaseDir, short int snIsShow,
                               int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);

  cv::namedWindow("7showImageSpine", CV_WINDOW_NORMAL);
cv:
  resizeWindow("7showImageSpine", showWidth, showHeight);

  char imgPath[256];

  sprintf(imgPath, "%s\\7showImageSpine\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("7showImageSpine", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("7showImageSpine", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::FinallyResultShow(Mat mSrcImageData, int picture_ID,
                                  char* pBaseDir, short int snIsShow,
                                  int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);
  cv::namedWindow("8FinallyResultShow", CV_WINDOW_NORMAL);
cv:
  resizeWindow("8FinallyResultShow", showWidth, showHeight);

  char imgPath[256];

  sprintf(imgPath, "%s\\8FinallyResultShow\\%d.jpg", pBaseDir, picture_ID);

  if (snIsShow == 0)
    imshow("8FinallyResultShow", mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow("8FinallyResultShow", mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::debugVanishingShow(Mat mSrcImageData, int picture_ID,
                                   char* pBaseDir, short int snIsShow,
                                   bool flag, int showWidth, int showHeight) {
  if (SHOW_INDEX)
    ShowTextinMat(mSrcImageData, Point(kImageWidth / 2, 20), picture_ID);
  char imgPath[256];

  String windowName = "VanishingPoint-Before";

  if (flag) {
    windowName = "VanishingPoint-After";
    sprintf(imgPath, "%s\\5TestVanishPoint\\%d.jpg", pBaseDir, picture_ID);
  } else {
    sprintf(imgPath, "%s\\4TestVanishPoint\\%d.jpg", pBaseDir, picture_ID);
  }

  cv::namedWindow(windowName, CV_WINDOW_NORMAL);
cv:
  resizeWindow(windowName, showWidth, showHeight);

  if (snIsShow == 0)
    imshow(windowName, mSrcImageData);
  else if (snIsShow == 1)
    imwrite(imgPath, mSrcImageData);
  else {
    imshow(windowName, mSrcImageData);
    imwrite(imgPath, mSrcImageData);
  }
}

void DebugShow::ShowTextinIplImage(IplImage* pImg, Point pt, int nId) {
  CvFont font;
  cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.5f, 1.5f, 0, 2,
             CV_AA);  //������ʾ������
  char strID[16];
  sprintf(strID, "%d", nId);
  cvPutText(pImg, strID, pt, &font, CV_RGB(255, 0, 0));  //��ɫ����ע��
}

void DebugShow::ShowTextinMat(Mat& Input, Point pt, int nId) {
  putText(Input, format("%d", nId), pt, FONT_HERSHEY_PLAIN, 1.5f,
          CV_RGB(255, 0, 0));
}

void DebugShow::ShowTextinMat(Mat& Input, Point pt, char* str) {
  putText(Input, str, pt, FONT_HERSHEY_PLAIN, 1.5f, CV_RGB(255, 0, 0));
}

void DebugShow::DrawTransCir(Mat& Input, CvPoint** pts, int* npts,
                             CvScalar color, int contours, double alpha) {
  //��mat->iplimage
  IplImage* img;  // = &IplImage(Input);
  int width = img->width;
  int height = img->height;

  IplImage* rec =
      cvCreateImage(cvSize(width, height), img->depth, img->nChannels);
  // cvRectangle(rec,cvPoint(100,100),cvPoint(width,height),color,-1);
  cvFillPoly(rec, pts, npts, contours, color);
  // cvSetImageROI(img, cvRect(0, 0, width, height));
  cvAddWeighted(img, alpha, rec, 1 - alpha, 0.0, img);
  cvResetImageROI(img);
  cvReleaseImage(&rec);
}

void DebugShow::DrawMaskinImage(Mat& Input, Mat maskInput, double alpha) {
  addWeighted(Input, alpha, maskInput, 1 - alpha, 0.0, Input);
}

void DebugShow::zyrLine(Mat Image, Point pt1, Point pt2, Scalar color,
                        int thickness, int lineType) {
  if (lineType == 0)  //ʵ��
  {
    line(Image, pt1, pt2, color, thickness);
  } else  //����
  {
    int deta_y = (pt2.y - pt1.y) / 7;
    int deta_x = (pt2.x - pt1.x) / 7;

    for (int k = 0; k < 8; k = k + 2) {
      Point pt_start, pt_end;

      pt_start.x = pt1.x + k * deta_x;
      pt_start.y = pt1.y + k * deta_y;

      pt_end.x = pt1.x + (k + 1) * deta_x;
      pt_end.y = pt1.y + (k + 1) * deta_y;

      line(Image, pt_start, pt_end, color, thickness);
    }
  }
}
}
